package com.orangelife.service.propFeeCommInfo.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.orangelife.exception.AppBizException;
import com.orangelife.service.cerHouseholder.ICerHouseholderService;
import com.orangelife.service.propFeeApartment.IPropFeeApartmentService;
import com.orangelife.service.propFeeBuilding.IPropFeeBuildingService;
import com.orangelife.service.propFeeComm.IPropFeeCommService;
import com.orangelife.service.propFeeUnit.IPropFeeUnitService;

public class HandleDeletedInfoThread extends Thread implements Runnable {

	private String threadName;
	private Map<String, Object> data;
	private int start;
	private int end;

	private IPropFeeApartmentService propFeeApartmentService;
	private IPropFeeBuildingService propFeeBuildingService;
	private ICerHouseholderService cerHouseholderService;
	private IPropFeeUnitService propFeeUnitService;

	private IPropFeeCommService propFeeCommService;

	public HandleDeletedInfoThread(String threadName, Map<String, Object> data,
			IPropFeeCommService propFeeCommService,
			IPropFeeBuildingService propFeeBuildingService,
			IPropFeeUnitService propFeeUnitService,
			IPropFeeApartmentService propFeeApartmentService,

			ICerHouseholderService cerHouseholderService) {
		this.threadName = threadName;
		this.data = data;
		this.propFeeCommService = propFeeCommService;
		this.propFeeBuildingService = propFeeBuildingService;
		this.propFeeUnitService = propFeeUnitService;
		this.propFeeApartmentService = propFeeApartmentService;
		this.cerHouseholderService = cerHouseholderService;
	}

	@SuppressWarnings("unchecked")
	public void run() {
		try {
			System.out.println(threadName + "\t\t\t\t\t 请求 数据" + "\t\t\t\t"
					+ JSONObject.fromObject(data).toString());

			if (data != null) {
				List<Map<String, Object>> comms = (List<Map<String, Object>>) data
						.get("comms");
				List<Map<String, Object>> buildings = (List<Map<String, Object>>) data
						.get("buildings");
				List<Map<String, Object>> units = (List<Map<String, Object>>) data
						.get("units");
				List<Map<String, Object>> apartments = (List<Map<String, Object>>) data
						.get("apartments");
				List<Map<String, Object>> owners = (List<Map<String, Object>>) data
						.get("owners");

				/***
				 * 删除小区
				 */
				if (comms != null && comms.size() > 0) {
					List<String> commIDs = new ArrayList<String>();
					for (Map<String, Object> map : comms) {
						commIDs.add(map.get("commID").toString());
					}
					propFeeCommService.bauthDeleteComm(commIDs);
				}

				/***
				 * 删除楼栋
				 */
				if (buildings != null && buildings.size() > 0) {

					List<String> buildingIDs = new ArrayList<String>();
					for (Map<String, Object> map : buildings) {
						buildingIDs.add(map.get("buildingID").toString());
					}
					propFeeBuildingService
							.bauthDeletePropFeeBuilding(buildingIDs);
				}
				/***
				 * 删除单元
				 */
				if (units != null && units.size() > 0) {
					List<String> unitIDs = new ArrayList<String>();
					for (Map<String, Object> map : units) {
						unitIDs.add(map.get("unitID").toString());
					}
					propFeeUnitService.bauthDeleteUnits(unitIDs);
				}
				/***
				 * 删除房子
				 */
				if (apartments != null && apartments.size() > 0) {

					List<String> apartmentIDs = new ArrayList<String>();
					for (Map<String, Object> map : apartments) {
						apartmentIDs.add(map.get("apartmentID").toString());
					}
					propFeeApartmentService
							.bauthDeletePropFeeApartment(apartmentIDs);
				}
				/***
				 * 删除业主
				 */
				if (owners != null && owners.size() > 0) {
					List<String> ownersIDs = new ArrayList<String>();
					for (Map<String, Object> map : owners) {
						ownersIDs.add(map.get("linkAptHouseholderID")
								.toString());
					}
					cerHouseholderService
							.bauthDeleteCerHouseholderLink(ownersIDs);
				}

			}

		} catch (AppBizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
